<?php
if (!Auth::check()) {
    ?>
<!-- Modal -->
<div class="modal fade" id="myModal-login" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">Đăng Nhập</h4>
            </div>
            
            <form action="" method="POST" role="form" id='login_form'>
                <div class="modal-body">
                    <input type="text" name="username" id="ipt-username" class="form-control"  required="required"   placeholder='Tài Khoản'>
                    <br/>
                    <input type="password" name="password" id="ipt-pw" class="form-control" required="required"   placeholder='Mật Khẩu'>
                    <div id='error'></div>
                </div>
                <div class="modal-footer">
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                            <button type="submit" class="btn btn-primary col-xs-6 col-sm-6 col-md-6 col-lg-6" style="width:49%;float:left">Đăng nhập</button>
                            <a href="<?php echo asset('user/create')?>" class="btn btn-default col-xs-6 col-sm-6 col-md-6 col-lg-6">Đăng ký tài khoản</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<?php
}
?>
<script type="text/javascript">
    $('#login_form').submit(function(event) {
        $('#error').hide();
        /* Act on the event */
        $.ajax({
            url: "<?php echo asset('user/login'); ?>",
            type: 'post',
            dataType: 'json',
            data: {
                username: document.getElementById('ipt-username').value,
                password: document.getElementById('ipt-pw').value
            },
        })
                .done(function(data) {
                    if (data == '1') {
                        $('#error').html('<br/><div class="alert alert-info fade in"><p>Đăng nhập thành công</p></div>');
                        $('#error').show();
                        window.location.reload();
                    } else {
                        $('#error').html('<br/><div class="alert alert-warning fade in"><p>Đăng nhập không thành công</p></div>');
                        $('#error').show();
                    }
                    console.log("success");
                })
                .fail(function(data) {
                    $('#error').html('<br/><div class="alert alert-warning fade in"><p>Đăng nhập không thành công</p></div>');
                        $('#error').show();
                })
                .always(function() {
                    console.log("complete");
                });

        return false;
    });
</script>

